const { getPlatformCode, getExtInfo } = require('./common/platform');
const { userLogin, userCheck, userRegister } = require('./api/member');
const { setUserInfo, setUserToken } = require('./biztools/info');

// 支持全局自定义事件监听
const event = require('./common/event');

// mixin 扩展支持
require('./common/mixins');

App({
    async onLaunch(options) {
        const extInfo = await getExtInfo();
        console.log('额外信息为：', extInfo);
        const { app_id, brand_id } = extInfo;
        // 拿取会员 openid
        const code = await getPlatformCode();
        console.log(code)
        let userInfo = await userLogin(code, app_id, brand_id);
        // 注册会员
        if (!userInfo.is_register) {
            userInfo = await userRegister(userInfo.open_id, {
                brandId: brand_id,
                appId: app_id,
            });
        }
        setUserInfo(userInfo);
        setUserToken(userInfo.token);
        this.globalData.isLogin = true;
        this.globalData.isShowedStartPage = false;
    },
    globalData: {
        config: null,
        setting: null,
        isLogin: false,
        brandInfo: null,
        isUserChooseAddress: false,
        isShowedStartPage: false,
        cardInfo: null,
        isNeedRefreshAddress: false,
        paySuccessUrl: '',
    },
    event: new event(),
})